﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using VIP.Extension;

public class VIPEncryptUtil {


    /// <summary>
    /// DES加密秘钥，为公司名称,英文不支持汉子
    /// </summary>
    private const string desKey = "qrrj";
    /// <summary>
    /// 默认密钥向量,产品名称
    /// </summary>
    private const string Keys = "qdhc";

    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="encryptString">待加密的字符串，未加密成功返回原串</param>
    /// <returns></returns>
    public static string EncryptDES(string encryptString)
    {
        try
        {
            byte[] rgbKey = ConfusingKey(desKey);
            byte[] rgbIV = ConfusingKey(Keys);
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch
        {
            return encryptString;
        }
    }

    /// <summary>
    /// 打乱混淆输入的Key
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    private static byte[] ConfusingKey(string s)
    {
        byte[] rgbKey = Encoding.UTF8.GetBytes(s);
        byte[] newBytes = new byte[8];
        for (int i = 0; i < 8; i++)
        {
            if (i < rgbKey.Length - 1)
            {
                newBytes[i] = rgbKey[1];
            }
        }
        byte nn = newBytes[5];
        newBytes[5] = newBytes[3];
        newBytes[3] = nn;
        return newBytes;
    }


    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="decryptString">待解密的字符串，未解密成功返回原串</param>
    /// <returns></returns>
    public static string DecryptDES(string decryptString)
    {
        try
        {
            byte[] rgbKey = DeconfusingKey(desKey);
            byte[] rgbIV = DeconfusingKey(Keys);
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            return decryptString;
        }
    }

    /// <summary>
    /// 反混淆输入的Key
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    private static byte[] DeconfusingKey(string s)
    {
        byte[] rgbKey = Encoding.UTF8.GetBytes(s);
        byte[] newBytes = new byte[8];
        for (int i = 0; i < 8; i++)
        {
            if (i < rgbKey.Length - 1)
            {
                newBytes[i] = rgbKey[1];
            }
        }
        byte nn = newBytes[3];
        newBytes[3] = newBytes[5];
        newBytes[5] = nn;
        return newBytes;
    }

    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="value">要加密的串</param>
    /// <returns></returns>
    public static string EncryptMD5(string decryptString)
    {
        MD5 md5Hash = MD5.Create();
        // 将输入字符串转换为字节数组并计算哈希数据 
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(decryptString));
        // 创建一个 Stringbuilder 来收集字节并创建字符串 
        StringBuilder sBuilder = new StringBuilder();
        // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        // 返回十六进制字符串 
        return sBuilder.ToString();
    }

    /// <summary>
    /// 机器码 = CPU序列号 + 硬盘ID + 网卡硬件地址
    /// </summary>
    /// <returns></returns>
    public static string GetMachineCode() {
        //获取MAC地址
        string machineCode = GetMACAddress();
        return machineCode;
    }

    /// <summary> 
    /// 获取cpu序列号 
    /// </summary> 
    /// <returns> string </returns> 
    private static string GetCpuInfo()
    {
        return null;
    }
    /// <summary> 
    /// 获取硬盘ID 
    /// </summary> 
    /// <returns> string </returns> 
    private static string GetHDid()
    {
        return null;
    }

    /// <summary> 
    /// 获取MAC地址
    /// </summary> 
    /// <returns> string </returns> 
    private static string GetMACAddress()
    {
        NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in nis)
        {
            if(ni.GetPhysicalAddress().ToString().IsNotEmpty())
            return ni.GetPhysicalAddress().ToString();
        }
        return null;
    }

    /// <summary>
    /// 凯撒加密
    /// </summary>
    /// <param name="decryptString"></param>
    /// <returns></returns>
    public static string EncryptCaesar(string encryptString, int key) {
        string strCaesar = null;
        return strCaesar;
    }

    /// <summary>
    /// 凯撒解密
    /// </summary>
    /// <param name="decryptString"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string DecryptCaesar(string decryptString, int key) {
        string strCaesar = null;
        return strCaesar;
    }

}
